layui.define(['element','layer'],function (exports) {
	var $ = layui.$,
	element = layui.element,
	layer = layui.layer,
	iname = '',
	Sidenav = function(){
		var t = this;
		t.config = {
			tips:true,
			callback:""
		}
	};
	Sidenav.prototype.set = function (option) {
		var t = this;
		$.extend(true, t.config,option);
		return t;
	},
	Sidenav.prototype.render = function(option){
		var t = this;
		var obj = $.extend(true, t.config,option);
		iname = $(obj.iframe).attr('name');
		if(iname.length>0){
			tt(obj);
			if(obj.tips){
				$(obj.dom).find('.name-text').each(function(i,ele){
					var font = [];
					var pw = $(ele).parent().width();
					var badge = $(ele).next(".f-badge").width();
					font.push($(ele).css('font-size'));
					font.push($(ele).css('font-family'));
					var fw = gw($(ele).text(),font.join(" "));
					var tpw = pw-badge;
					if($(ele).prev().hasClass("iconfont") || $(ele).prev().hasClass("f-ng-p")){
						tpw-=30;
					}
					if(fw>tpw){
						$(ele).parent().addClass("badge-right");
					}else{
						$(ele).parent().removeClass("badge-right");
					}
					$(ele).parent().hover(function(){
						var font = [];
						var pw = $(ele).parent().width();
						font.push($(ele).css('font-size'));
						font.push($(ele).css('font-family'));
						var fw = gw($(ele).text(),font.join(" "));
						var tpw = pw;
						if($(ele).prev().hasClass("iconfont") || $(ele).prev().hasClass("f-ng-p")){
							tpw-=30;
						}
						if(fw>tpw){
							layer.tips($(this).find('.name-text').text(),this,{time:0})
						}
					},
					function(){
						layer.close(layer.index);
					})
				})
			}
			element.on('nav('+obj.filter+')', function(elem){
				if(obj.callback){
					obj.callback(elem);
				}
			});
		}else{
			console.log('iframe没有设置name属性');
		}
	}
	var cc = function(obj,show){
			var dom = '<dl class="layui-nav-child">';
			for(var i=0;i<obj.length;i++){
				if(show == obj[i].name || show == obj[i].url){
					dom +='<dd class="layui-this">';
				}else{
					dom +='<dd>';
				}
				dom +='<a href="'+(obj[i].url?obj[i].url:'javascript:;')+'" '+(obj[i].url?('target="'+(obj[i].target?obj[i].target:iname)+'"'):'')+'>'+(obj[i].icon?'<i class="'+obj[i].icon+'"></i>':'')+'<span class="name-text">'+obj[i].name + (obj[i].badge?'</span><span class="f-badge">'+obj[i].badge+'</span>':'')+'</a>';
				
				if(obj[i].hasOwnProperty('children')){
					var children = obj[i].children;
					dom += cc(children,show);
				}
				dom +='</dd>';
			}
			dom +='</dl>';
			return dom;
		},
	ss = function(obj,show){
		var html = '';
		if(show.indexOf('?')>-1){
			show = show.replace(show.substring(show.indexOf('?'),show.length),'');
			for(var i=0;i<obj.length;i++){
				if(show == obj[i].name || show == obj[i].url){
					html +='<li class="layui-nav-item layui-this">';
				}else{
					html +='<li class="layui-nav-item">';
				}
				html +='<a href="'+(obj[i].url?obj[i].url:'javascript:;')+'" '+(obj[i].url?('target="'+(obj[i].target?obj[i].target:iname)+'"'):'')+'><i class="'+(obj[i].icon?obj[i].icon+' iconfont':'f-ng-p')+'"></i><span class="name-text">'+obj[i].name +(obj[i].badge?'</span><span class="f-badge">'+obj[i].badge+'</span>':'')+'</a>';
				if(obj[i].hasOwnProperty('children')){
					var children = obj[i].children;
					html += cc(children,show);
				}
				html +='</li>';
			}
			return html;
		}else{
			if(show.indexOf('#')>-1){
				show = show.replace(show.substring(show.indexOf('#'),show.length),'');
			}
			if(show.indexOf('/projectApply/toIndex.html')>-1){
				show = show.replace('/projectApply/toIndex.html','/projectApply/toApplyList.html');
			}
			for(var i=0;i<obj.length;i++){
				if(show == obj[i].name || show == obj[i].url){
					html +='<li class="layui-nav-item layui-this">';
				}else{
					html +='<li class="layui-nav-item">';
				}
				html +='<a href="'+(obj[i].url?obj[i].url:'javascript:;')+'" '+(obj[i].url?('target="'+(obj[i].target?obj[i].target:iname)+'"'):'')+'><i class="'+(obj[i].icon?obj[i].icon+' iconfont':'f-ng-p')+'"></i><span class="name-text">'+obj[i].name +(obj[i].badge?'</span><span class="f-badge">'+obj[i].badge+'</span>':'')+'</a>';
				if(obj[i].hasOwnProperty('children')){
					var children = obj[i].children;
					html += cc(children,show);
				}
				html +='</li>';
			}
			return html;
		}
	},
	gw =function(text, font) {
        var currentObj = $('<pre>').hide().appendTo(document.body);
        $(currentObj).html(text).css('font', font);
        var width = currentObj.width();
        currentObj.remove();
        return width;
    },
	tt = function(obj){
		var json=obj.json;
		var html = ss(json,obj.show);
		$(obj.dom).html(html);
		var thisdom = $(obj.dom).find(".layui-this");
		if(thisdom.parents('dd').length>0){
			thisdom.parents('dd').addClass('layui-nav-itemed layui-nav-this');
		}
		if(thisdom.parents('.layui-nav-item').length>0){
			thisdom.parents('.layui-nav-item').addClass('layui-nav-itemed layui-nav-active');
		}
		element.render();
		if(obj.show){
			$(obj.iframe).attr("src",obj.show);
		}else{
			if(obj.json[0].url){
				$(obj.iframe).attr("src",obj.json[0].url);
			}else{
				$(obj.iframe).attr("src",obj.json[0].children[0].url);
			}
		}
	};
	var sidenav = new Sidenav();
	exports('sidenav',sidenav);
})